package ru.papergames
{
	import by.andronix.logger.Logger;
	import by.andronix.logger.LogTargetFactory;
	import fr.kapit.puremvc.as3.patterns.facade.DebugFacade;
	import org.puremvc.as3.interfaces.IFacade;
	import ru.papergames.controller.commands.*;
	import ru.papergames.controller.commands.StartupCommand;
	import ru.papergames.controller.notifications.CoreNotifications;
	import ru.papergames.controller.notifications.FrontendNotifications;
	import ru.papergames.model.proxies.LoginProxy;
	import ru.papergames.model.proxies.MainHTTPProxy;
	import ru.papergames.model.proxies.PersistentProxy;
	import ru.papergames.model.proxies.SettingsProxy;
	
		
	
	//public class MainFacade extends Facade implements IFacade
	public class MainFacade extends DebugFacade implements IFacade
	{				
		
		public static const STARTUP:String = 'MainFacade.STARTUP_COMMAND';
		
		
		public static function getInstance():MainFacade {
			if (instance == null) instance = new MainFacade();
			return instance as MainFacade;
		}
		
		override protected function initializeFacade():void {
				
			Logger.addLog(LogTargetFactory.FIREBUG_TARGET, ['ru.papergames.*']);
			//Logger.addLog(LogTargetFactory.KP_PUREMVC_CONSOLE_TARGET, ['ru.papergames.*']);
								
			Logger.getLogger(this).debug('initializeFacade');	
			
			super.initializeFacade();			
		}
				
		override protected function initializeController():void
		{
			super.initializeController();
			
			registerCommand(STARTUP, StartupCommand);
			registerCommand(CoreNotifications.INITIALIZE_MESSAGING, InitializeMessagingCommand);
			registerCommand(CoreNotifications.LOAD_CONFIG, LoadConfigCommand);
			registerCommand(SettingsProxy.SETTINGS_LOAD_SUCCESS, InitializeCommand);
						
			registerCommand(FrontendNotifications.TRY_LOGIN, LoginCommand);
			registerCommand(LoginProxy.LOGIN_SUCCESS, LoginCommand);
			
			registerCommand(FrontendNotifications.TRY_TO_REGISTER, RegisterCommand);
					
						
			registerCommand(FrontendNotifications.LOGOUT, LogoutCommand);
						
			Logger.getLogger(this).debug('initializeController Complete');			
		}
		
		override protected function initializeModel():void {
			super.initializeModel();
			
			MainHTTPProxy.topFacade = this;
			
			this.registerProxy(new PersistentProxy(true));
			this.registerProxy(new SettingsProxy());
		}
		
		override public function sendNotification(notificationName:String, body:Object=null, type:String=null):void {
			super.sendNotification(notificationName, body, type);
			
			Logger.getLogger(this).debug('Facace::sendNotification(): ' + notificationName);
		}
		
		public function startup( app:Main ):void
		{	
			
			Logger.getLogger(this).debug('Invoking startup...');				
							
			sendNotification(STARTUP, app);			
		}	
	}
}